﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WrapperTest.ServiceReference;

namespace WrapperTest
{

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private WrapperServiceClient _service;


        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //var _init = new Dictionary<String, String>();
            //_init.Add("app_id", "427107");
            //_init.Add("session_key", "6a8b81585d506bd3b75aeb9802fcf930");
            //_init.Add("authentication_key", "7d9cb73b559fe3c481e6742418ecc937");
            //_init.Add("is_app_user", "1");
            //_init.Add("oid", "13522930837595030077");
            //_init.Add("vid", "13522930837595030077");
            //_init.Add("window_id", "CometName_bae8b65a01b07eefcb0c47e5375ae99d");

            _service = new WrapperServiceClient();
            _service.SetInitsParamCompleted += ServiceSetInitsParamCompleted;
            tab.IsEnabled = false;
            _service.SetInitsParamAsync(HtmlPage.Document.QueryString.ToDictionary(k => k.Key, v => v.Value));
            //_service.SetInitsParamAsync(_init);
            cbx_usersHasAppPermission.Items.Add(AppPermission.News);
            cbx_usersHasAppPermission.Items.Add(AppPermission.Notice);
            cbx_usersHasAppPermission.SelectedIndex = 0;

            _service.FriendsGetCompleted += ServiceFriendsGetCompleted;
            _service.FriendsGetAppUsersCompleted += ServiceFriendsGetAppUsersCompleted;
            _service.UsersGetInfoCompleted += ServiceUsersGetInfoCompleted;
            _service.UsersIsAppUserCompleted += ServiceUsersIsAppUserCompleted;
            _service.UsersHasAppPermissionCompleted += ServiceUsersHasAppPermissionCompleted;
            _service.PaymentsOpenDialogSmsCompleted += ServicePaymentsOpenDialogSmsCompleted;
            _service.PaymentsOpenDialogOtherCompleted += ServicePaymentsOpenDialogOtherCompleted;
            _service.StreamPublishCompleted += ServiceStreamPublishCompleted;
            _service.PhotosGetAlbumsCompleted += ServicePhotosGetAlbumsCompleted;
            _service.PhotosGetCompleted += ServicePhotosGetCompleted;
            _service.AudiosGetCompleted += ServiceAudiosGetCompleted;
            _service.AudiosLinkAudioCompleted += ServiceAudiosLinkAudioCompleted;
        }

        void ServiceSetInitsParamCompleted(object sender, SetInitsParamCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }
            tab.IsEnabled = true;
        }

        private void InvokeFriendsGet(object sender, RoutedEventArgs e)
        {
            ((Button) sender).IsEnabled = false;
            _service.FriendsGetAsync(list_users.SelectedItem == null ? null : ((User) list_users.SelectedItem).Uid, cb_friendsGet.IsChecked.Value,sender);
        }
        void ServiceFriendsGetCompleted(object sender, FriendsGetCompletedEventArgs e)
        {
            ((Button) e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
               
                return;
            }
            list_users.ItemsSource = e.Result;
        }

        private void InvokeFriendsGetAppUsers(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.FriendsGetAppUsersAsync(list_users.SelectedItem == null ? null : ((User)list_users.SelectedItem).Uid, cb_friendsGetAppUsers.IsChecked.Value, sender);
        }
        void ServiceFriendsGetAppUsersCompleted(object sender, FriendsGetAppUsersCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            list_users.ItemsSource = e.Result;
        }

        private void InvokeUsersGetInfo(object sender, RoutedEventArgs e)
        {
            if (list_users.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select users first");
                return;
            }
            ((Button) sender).IsEnabled = false;
            _service.UsersGetInfoAsync(list_users.SelectedItems.Cast(u => ((User) u).Uid).ToList(), sender);
        }
        void ServiceUsersGetInfoCompleted(object sender, UsersGetInfoCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            list_users.ItemsSource = e.Result;
        }

        private void InvokeUsersIsAppUser(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.UsersIsAppUserAsync(list_users.SelectedItem == null ? null : ((User)list_users.SelectedItem).Uid, sender);
        }
        static void ServiceUsersIsAppUserCompleted(object sender, UsersIsAppUserCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            MessageBox.Show("UsersIsAppUser result: " + e.Result);
        }

        private void InvokeUsersHasAppPermission(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.UsersHasAppPermissionAsync((AppPermission)cbx_usersHasAppPermission.SelectedItem, list_users.SelectedItem == null ? null : ((User)list_users.SelectedItem).Uid, sender);
        }
        static void ServiceUsersHasAppPermissionCompleted(object sender, UsersHasAppPermissionCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            MessageBox.Show("UsersHasAppPermission result: " + e.Result);
        }

        private void InvokePaymentsOpenDialogSms(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.PaymentsOpenDialogSmsAsync((int) nud_playmentsSmsServiceId.Value, 
                                                tb_playmentsSmsServiceName.Text,
                                                (int) nud_playmentsSmsPrice.Value, sender);
        }
        static void ServicePaymentsOpenDialogSmsCompleted(object sender, PaymentsOpenDialogSmsCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            MessageBox.Show("PaymentsOpenDialogSms result: " + e.Result);
        }

        private void InvokePaymentsOpenDialogOther(object sender, RoutedEventArgs e)
        {
            ((Button) sender).IsEnabled = false;
            _service.PaymentsOpenDialogOtherAsync((int)nud_playmentsOtherServiceId.Value,
                                                tb_playmentsOtherServiceName.Text,
                                                (int) nud_playmentsOtherPrice.Value, sender);
        }
        static void ServicePaymentsOpenDialogOtherCompleted(object sender, PaymentsOpenDialogOtherCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            MessageBox.Show("PaymentsOpenDialogOther result: " + e.Result);
        }

        private void InvokeStreamPublish(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.StreamPublishAsync(tb_streamText.Text, tb_streamPost.Text, (int) nud_streamImg.Value, sender);

        }
        static void ServiceStreamPublishCompleted(object sender, StreamPublishCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            MessageBox.Show("StreamPublish result: " + e.Result);
        }

        private void InvokePhotosGetAlbums(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.PhotosGetAlbumsAsync(list_users.SelectedItem == null ? null : ((User)list_users.SelectedItem).Uid, list_photoAlbums.SelectedItems.Cast(u => ((PhotoAlbum)u).Aid).ToList(),sender);
        }
        void ServicePhotosGetAlbumsCompleted(object sender, PhotosGetAlbumsCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            list_photoAlbums.ItemsSource = e.Result;
        }

        private void InvokePhotosGet(object sender, RoutedEventArgs e)
        {
            if (list_photoAlbums.SelectedItem == null)
            {
                MessageBox.Show("Select photo album first");
                return;
            }
            ((Button) sender).IsEnabled = false;
            _service.PhotosGetAsync(((PhotoAlbum) list_photoAlbums.SelectedItem).Aid,
                                    list_users.SelectedItem == null ? null : ((User) list_users.SelectedItem).Uid,
                                    list_photos.SelectedItems.Cast(u => ((Photo) u).Pid).ToList(), sender);
        }
        void ServicePhotosGetCompleted(object sender, PhotosGetCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            list_photos.ItemsSource = e.Result;
        }

        private void InvokeAudiosGet(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            _service.AudiosGetAsync(list_users.SelectedItem == null ? null : ((User)list_users.SelectedItem).Uid,
                                    list_audios.SelectedItems.Cast(u => ((Audio)u).Mid).ToList(), sender);

        }
        void ServiceAudiosGetCompleted(object sender, AudiosGetCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            list_audios.ItemsSource = e.Result;
        }

        private void InvokeAudiosLinkAudio(object sender, RoutedEventArgs e)
        {
            if (list_audios.SelectedItem == null)
            {
                MessageBox.Show("Select audio first");
                return;
            }
            ((Button)sender).IsEnabled = false;
            _service.AudiosLinkAudioAsync(((Audio)list_audios.SelectedItem).Mid);
        }
        static void ServiceAudiosLinkAudioCompleted(object sender, AudiosLinkAudioCompletedEventArgs e)
        {
            ((Button)e.UserState).IsEnabled = true;
            if (e.Error != null)
            {
                if (e.Error is FaultException<FaultMailRuWrapperRequestException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperRequestException>;
                    MessageBox.Show(_ex.Detail.Message + " " + _ex.Detail.Code + " " + _ex.Detail.Request);
                }
                else if (e.Error is FaultException<FaultMailRuWrapperException>)
                {
                    var _ex = e.Error as FaultException<FaultMailRuWrapperException>;
                    MessageBox.Show(_ex.Detail.Message);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }
                return;
            }
            MessageBox.Show("AudiosLinkAudio result: " + e.Result);
        }

        private void list_users_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _list_usersGetInfo.ItemsSource = list_users.SelectedItems;
        }

        private void list_photoAlbums_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _list_photoAlbumsPhotosGetAlbums.ItemsSource = list_photoAlbums.SelectedItems;
        }

        private void list_photos_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _list_photosPhotosGet.ItemsSource = list_photos.SelectedItems;
        }

        private void list_audios_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _list_audiosAudiosGet.ItemsSource = list_audios.SelectedItems;
        }
    }

}
